
package com.kanjian.music.fragment;

import java.util.ArrayList;

import com.kanjian.music.R;
import com.kanjian.music.activity.BaseActivity;
import com.kanjian.music.activity.RankActivity;
import com.kanjian.music.adapter.MusicListListAdapater;
import com.kanjian.music.constants.IntentConstants;
import com.kanjian.music.constants.MusicListTypeConstants;
import com.kanjian.music.dialog.ShareDialog;
import com.kanjian.music.download.DownloadManager;
import com.kanjian.music.download.MusicDownloadTask;
import com.kanjian.music.entity.Music;
import com.kanjian.music.util.PlayerUtil;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class MusicListFragment extends BaseFragment {

    public static MusicListFragment newInstance(ArrayList<Music> musicList) {
        Bundle b = new Bundle();
        b.putSerializable(IntentConstants.MUSICLIST, musicList);
        MusicListFragment f = new MusicListFragment();
        f.setArguments(b);
        return f;
    }

    private View mRootView;

    private ArrayList<Music> mMusicList;

    private TextView mBtn_playAll;

    private TextView mBtn_addAll;

    private TextView mBtn_downloadAll;

    private ListView mListView;

    private MusicListListAdapater mListAdapater;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle b = getArguments();
        if (b != null) {
            mMusicList = (ArrayList<Music>) b.getSerializable(IntentConstants.MUSICLIST);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        initView(inflater);
        return mRootView;
    }

    private void initView(LayoutInflater inflater) {
        if (mRootView == null) {
            mRootView = inflater.inflate(R.layout.fragment_music_list, null);
        }
        mBtn_playAll = (TextView) mRootView.findViewById(R.id.musiclist_controler_play_all);
        mBtn_addAll = (TextView) mRootView.findViewById(R.id.musiclist_controler_add_all);
        mBtn_downloadAll = (TextView) mRootView.findViewById(R.id.musiclist_controler_cache_all);
        mBtn_addAll.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // 添加全部
                PlayerUtil.addMusicList(mMusicList);
            }
        });
        mBtn_playAll.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // 播放全部
                PlayerUtil.addMusicListThenPlay(mMusicList,
                        ((BaseActivity) getActivity()).mPlayService);
            }
        });
        mBtn_downloadAll.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // 下载全部
                ArrayList<MusicDownloadTask> taskList = new ArrayList<MusicDownloadTask>(mMusicList
                        .size());
                MusicDownloadTask task;
                for (int i = 0; i < mMusicList.size(); i++) {
                    task = new MusicDownloadTask(mMusicList.get(i), 1, true);
                    taskList.add(task);
                }
                DownloadManager.addTaskList(taskList);
            }
        });
        // mBtn_share = (Button)
        // mRootView.findViewById(R.id.musiclist_controler_share);
        // mBtn_share.setOnClickListener(new OnClickListener() {
        // @Override
        // public void onClick(View v) {
        // if (getActivity() instanceof RankActivity) {
        // String rankName = null;
        // switch (((RankActivity) getActivity()).mMusicListType) {
        // case MusicListTypeConstants.MUSIC_LIST_TYPE_HOT:
        // rankName = "24小时热门榜";
        // break;
        // case MusicListTypeConstants.MUSIC_LIST_TYPE_MONTH:
        // rankName = "最近30天";
        // break;
        // case MusicListTypeConstants.MUSIC_LIST_TYPE_WEEK:
        // rankName = "最近7天";
        // break;
        // case MusicListTypeConstants.MUSIC_LIST_TYPE_NEW:
        // rankName = "新晋榜";
        // break;
        // default:
        // rankName = "24小时热门榜";
        // break;
        // }
        // ShareDialog.newInstance(rankName).show(getFragmentManager(), "");
        // }
        // }
        // });
        mListView = (ListView) mRootView.findViewById(R.id.musiclist_listview);
        mListAdapater = new MusicListListAdapater((BaseActivity) getActivity(), mListView,
                mMusicList);
        mListView.setAdapter(mListAdapater);
    }

    public void setMusicList(ArrayList<Music> musicList) {
        this.mMusicList = musicList;
        this.mListAdapater.setMusicList(mMusicList);
    }

}
